import db from '../../config/sqlcfg';

class EthTransaction {
  
  private static table: string = 'eth_transaction';
  
  /**
   * 保存交易数据
   * @param {string} hash
   * @param {number} block_num
   * @param {(string|number)} from
   * @param {(string|number)} to
   * @param {(number|string)} amount
   * @param {number} coinType
   * @returns
   * @memberof EthTransaction
   */
  static async save(hash: string, block_num: number, from: string|number, to: string|number, amount: number|string, coinType: number) {
    let ther = await db.query(`select count(id) as num from ${this.table} where transaction_hash = '${hash}'`);
    if(ther[0].num){
      await db.query(`update ${this.table} set affert_num = affert_num + 1 where transaction_hash = '${hash}'`);
      return false;
    }else{
      await db.query(`insert into ${this.table} set 
        block_num = ${block_num},
        transaction_hash = '${hash}',
        amount = ${amount},
        \`from\` = '${from}',
        \`to\` = '${to}',
        coin_type = ${coinType}
        `);
      return true;
    }
  }

}

export default EthTransaction;